package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.mapper.CompanyMapper;
import com.team.recruitment_management_system.mapper.SysUserMapper;
import com.team.recruitment_management_system.pojo.dto.hr.AddHrInfoDto;
import com.team.recruitment_management_system.pojo.dto.hr.ChangeHrInfoDto;
import com.team.recruitment_management_system.pojo.dto.hr.HrPageQuery;
import com.team.recruitment_management_system.pojo.entity.Company;
import com.team.recruitment_management_system.pojo.entity.Hr;
import com.team.recruitment_management_system.pojo.entity.SysUser;
import com.team.recruitment_management_system.pojo.vo.CompanyInfoVo;
import com.team.recruitment_management_system.pojo.vo.HrInfoVo;
import com.team.recruitment_management_system.service.HrService;
import com.team.recruitment_management_system.mapper.HrMapper;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.result.PageResult;
import com.team.recruitment_management_system.util.result.Result;
import com.team.recruitment_management_system.util.result.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86147
 * @description 针对表【hr(HR表)】的数据库操作Service实现
 * @createDate 2024-03-21 20:28:40
 */
@Service
public class HrServiceImpl extends ServiceImpl<HrMapper, Hr>
        implements HrService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private HrMapper hrMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<Void> addHrInfo(AddHrInfoDto addHrInfoDto) {
        if (!isUserExists(addHrInfoDto.getUserId())) {
            return Result.build(null, ResultCodeEnum.USER_ERROR);
        } else if (!isCompanyExists(addHrInfoDto.getCompanyId())) {
            return Result.build(null, 500, "公司不存在");
        }
        Hr hr = new Hr();
        BeanUtils.copyProperties(addHrInfoDto, hr);
        this.save(hr);
        return Result.postOk(null);
    }

    @Override
    public Result<Void> changeHrInfo(ChangeHrInfoDto changeHrInfoDto) {
        Hr hr = this.getById(changeHrInfoDto.getId());
        if (hr == null) {
            return Result.build(null, 500, "hr不存在");
        }
        BeanUtils.copyProperties(changeHrInfoDto, hr);
        this.updateById(hr);
        return Result.putOk(null);
    }

    @Override
    public Result<Void> deleteHrById(int hrId) {
        Hr hr = this.getById(hrId);
        if (hr == null) {
            return Result.build(null, 500, "hr不存在");
        }
        this.removeById(hr);
        return Result.deleteOk(null);
    }

    @Override
    public PageResult<HrInfoVo> getHrInfoPage(HrPageQuery hrPageQuery) {
        Page<Hr> hrPage = new Page<>(hrPageQuery.getPageNum(), hrPageQuery.getPageSize());
        IPage<HrInfoVo> hrInfoPage = hrMapper.getHrInfoPage(hrPage, hrPageQuery);
        return PageResult.success(hrInfoPage);
    }

    @Override
    public Result<HrInfoVo> getHrInfoById(int hrId) {
        if (this.getById(hrId) == null) {
            return Result.build(null, 500, "hr不存在");
        }
        HrInfoVo hrInfoVo = hrMapper.getHrInfoById(hrId);
        return Result.ok(hrInfoVo);
    }

    @Override
    public Result<HrInfoVo> getMyHrInfo(String token) {
        Integer userId = jwtHelper.getUserId(token);
        HrInfoVo hrInfoVo = hrMapper.getHrInfoByUserId(userId);
        if (hrInfoVo == null) {
            return Result.build(null, 500, "hr不存在");
        }
        return Result.ok(hrInfoVo);
    }

    @Override
    public Result<Integer> isHr(int id) {
        // 创建一个 QueryWrapper 对象
        QueryWrapper<Hr> queryWrapper = new QueryWrapper<>();
        // 使用条件构造器设置查询条件，这里是根据指定字段进行匹配
        queryWrapper.eq("user_id", id);
        // 调用 MyBatis Plus 提供的方法进行查询
        List<Hr> hrList = baseMapper.selectList(queryWrapper);
        if(hrList != null && hrList.size() >= 1){
            return Result.ok(1);
        }
        return Result.ok(0);
    }

    private boolean isUserExists(int userId) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, userId);
        return sysUserMapper.exists(queryWrapper);
    }

    private boolean isCompanyExists(int companyId) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getId, companyId);
        return companyMapper.exists(queryWrapper);
    }



}




